package com.sixiaolong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sixiaolong.common.CustomException;
import com.sixiaolong.dao.CategoryDao;
import com.sixiaolong.domain.Category;
import com.sixiaolong.domain.Dish;
import com.sixiaolong.domain.Setmeal;
import com.sixiaolong.service.CategoryService;
import com.sixiaolong.service.DishService;
import com.sixiaolong.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {

//    注入菜品service和套餐service
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    //    根据id删除分类，删除之前先判断是否已绑定
    @Override
    public void remove(Long id) {
//        查询当前分类是否关联了菜品，如果已经关联，抛出业务异常
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Dish::getCategoryId,id);
        int count = dishService.count(lqw);
        if (count > 0){
//        已经关联菜品需要抛出异常
            throw new CustomException("当前分类关联了菜品，不能删除");
//            return R.error("前分类关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Setmeal::getCategoryId,id);
        int count1 = setmealService.count(lqw1);
        if (count1 > 0){
//        查询当前分类是否关联了套餐
            throw new CustomException("当前分类关联了套餐，不能删除");
//            return R.error("前分类关联了套餐，不能删除");
        }

//        删除
        super.removeById(id);
//        return R.success("删除成功");
    }
}
